package com.beijingBorderCheck.service;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

/**
 * Servlet implementation class ServiceRuqest
 */
@WebServlet("/ServiceRuqest")
public class ServiceRuqest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void setCrossHeader(HttpServletResponse response) {
		
		 response.setHeader("Access-Control-Allow-Origin", "*");   
		 response.setContentType("application/json;charset=utf-8");
		 response.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
//		 response.setHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS, DELETE");
		 response.setHeader("Access-Control-Allow-Headers","DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept-Language, Origin, Accept-Encoding, accesstoken");
		 
//		response.setHeader("Access-Control-Allow-Headers", "content-type, accept");
//		response.setHeader("Access-Control-Allow-Methods", "POST");
//		response.setStatus(200);
//		response.setContentType("application/json;charset=utf-8");
//		response.setCharacterEncoding("utf-8");
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("static-access")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		setCrossHeader(response);

		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("application/json; charset=UTF-8");
		
		String data = request.getParameter("code").replace(" ", "+");
		System.out.println(data);
		//异常抛出
		JSONObject jsObject = new JSONObject();
		//执行
		String decryptQRCode = "";
		try {			
			 String unzip = ZipUtils.gunzip(data);
			 decryptQRCode = DESUtil.decrypt(unzip);
			 decryptQRCode = new String( decryptQRCode.getBytes( "GBK" ), "utf8" );
//			System.out.println("1---------"+decryptQRCode);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("codeData", decryptQRCode);
//			System.out.println("2---------"+decryptQRCode);
			if (decryptQRCode.equals("")) {
				//返回
				jsObject.put("code", 1);
				jsObject.put("success", true);
				jsObject.put("data", jsonObject);
				jsObject.put("message", "数据解析失败");
			}else {
				//返回
				jsObject.put("code", 0);
				jsObject.put("success", true);
				jsObject.put("data", jsonObject);
				jsObject.put("message", "执行成功");
			}
		} catch (Exception e) {
			//TODO:handle exception
			e.printStackTrace();
			jsObject.put("code", -1);
			jsObject.put("success", false);
			jsObject.put("message", "执行失败");
			jsObject.put("data", new HashMap<>());
		}
//		response.setHeader("Content-type", "text/html;charset=UTF-8");   
//		PrintWriter writer = response.getWriter();
		OutputStream os = response.getOutputStream();
		os.write(jsObject.toJSONString().getBytes("UTF-8"));
		os.flush();
		System.out.println(jsObject.toJSONString());
	}

}
